package com.demo.project_template.service.impl;

import com.demo.project_template.entity.Gift;
import com.demo.project_template.entity.UnlockWechat;
import com.demo.project_template.entity.User;
import com.demo.project_template.entity.UserGift;
import com.demo.project_template.exception.ErrorCodeException;
import com.demo.project_template.mapper.GiftMapper;
import com.demo.project_template.mapper.UnlockWechatMapper;
import com.demo.project_template.mapper.UserMapper;
import com.demo.project_template.service.UnlockWechatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.project_template.service.UserGiftService;
import com.demo.project_template.util.CheckEmptyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 解锁微信记录表 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 11:48:57
 */
@Service
public class UnlockWechatServiceImpl extends ServiceImpl<UnlockWechatMapper, UnlockWechat> implements UnlockWechatService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private GiftMapper giftMapper;
    @Resource
    private UserGiftService userGiftService;

    @Override
    public void unlock(UnlockWechat unlockWechat) {
        //判断该女用户是否添加微信
        User woman = userMapper.selectById(unlockWechat.getUnlockUserId());
        if (CheckEmptyUtil.isEmpty(woman.getWechatAccount())){
            throw new ErrorCodeException("该用户未设置微信账号");
        }

        //判断礼物是否为解锁微信的礼物
        Gift gift = giftMapper.selectById(unlockWechat.getGiftId());
        if (!"2".equals(gift.getType())){
            throw new ErrorCodeException("礼物错误");
        }

        //男性用户向女性用户送礼
        UserGift userGift = new UserGift();
        userGift.setSendUserId(unlockWechat.getUserId());
        userGift.setUserId(unlockWechat.getUnlockUserId());
        userGift.setGiftId(unlockWechat.getGiftId());
        userGiftService.sendGift(userGift);

        //添加解锁微信记录
        unlockWechat.setUnlockWechatAccount(woman.getWechatAccount());
        unlockWechat.setState("0");
        save(unlockWechat);

        //todo 向女用户发送申请获得微信的消息
    }
}
